//
//  FadebackVC.swift
//  ShareUmbrella
//
//  Created by niupark on 2017/8/12.
//  Copyright © 2017年 尧尚信息科技. All rights reserved.
//

import UIKit
import YSKit
import TSMessages

class FadebackVC: YSBaseViewController,UIScrollViewDelegate,UITextViewDelegate {
    
    let kScreenHeight = UIScreen.main.bounds.size.height
    let kScreenWidth = UIScreen.main.bounds.size.width
    
    var scrollView = UIScrollView()
    var textView = UITextView()
    var placeholderLabel = UILabel()
    
    override func setup() {
        super.setup()
    }
    /*
     *
     * 界面布局初始化
     */
    override func buildUI() {
        super.buildUI()
        self.title = I18n("VC.LFSide.fadeback.titlte")
        
        self.addRightBtnItem()
        self.addScrollView()
        self.addContentView()
        self.addPlaceholderLabel()

    }
    
    //
    override func buildRx() {
        super.buildRx()
    }
    
    //RGB
    func RGB(r:CGFloat,_ g:CGFloat,_ b:CGFloat) -> UIColor{
        return UIColor(red: (r)/255.0, green: (g)/255.0, blue: (b)/255.0, alpha: 1.0)
    }
    
    //提交
    func addRightBtnItem(){
        let btn1=UIButton(frame: CGRect(x: 0, y: 0, width: 50, height: 30))
        btn1.setTitle("提交", for: UIControlState.normal)
        btn1.addTarget(self, action: #selector(submit), for: UIControlEvents.touchUpInside)
        let item2=UIBarButtonItem(customView: btn1)
        self.navigationItem.rightBarButtonItem=item2
    }
    
    func addScrollView(){
        scrollView = UIScrollView.init(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: kScreenWidth-64))
        scrollView.delegate = self
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.showsVerticalScrollIndicator = false
        scrollView.contentSize = CGSize(width:kScreenWidth,height:kScreenHeight-63)
        self.view.addSubview(scrollView)
        
    }
    
    //添加textview
    func addContentView(){
        
        textView = UITextView(frame: CGRect(x: 10, y: 20, width: kScreenWidth-20, height: 200))
        textView.delegate = self as UITextViewDelegate
        textView.backgroundColor = UIColor.clear
        textView.font = UIFont.systemFont(ofSize: 16)
        textView.becomeFirstResponder()
        scrollView.addSubview(textView)
    }
    
    func addPlaceholderLabel(){
        placeholderLabel = UILabel.init()
        placeholderLabel.backgroundColor = UIColor.clear;
        placeholderLabel.frame = CGRect(x:13, y:17, width:kScreenWidth, height:40)
        placeholderLabel.text = "请输入您的反馈意见(150字以内)"
        placeholderLabel.backgroundColor = UIColor.clear
        placeholderLabel.textColor = UIColor.lightGray
        placeholderLabel.font = UIFont.systemFont(ofSize: 14)
        scrollView.addSubview(placeholderLabel)
    }
    
    //提交事件
    func submit(){
        if (textView.text.lengthOfBytes(using: String.Encoding.utf8) <= 0){
            return ;
        }
        
        textView.resignFirstResponder()
        HUD.show()
        _ = YSNetWorkManager.provider.requestJSON(YSStructTarget(UserAPI.sendFeedbackConment(token: Auth.token, conment: textView.text))).subscribe(onNext: {(data) in
            //
            HUD.hide()
            //延迟1s返回上一层
            let retcode = data?["retcode"].int
            if(retcode == 0){
                //HUD.show(HudIcon.success, title: "发送成功")
                TSMessage.showNotification(withTitle: I18n("发送成功"), type: .success)
                DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + Double(Int64(1*NSEC_PER_SEC))/Double(NSEC_PER_SEC)) {
                    self.navigationController?.popViewController(animated: true)
                }
            }else{
                HUD.show(HudIcon.failure, title: "失败")
            }
            
        }, onError: {(_) in
            HUD.hide()
        })
    }
    
    //MARK TextView Delegate
    func textViewDidChange(_ textView: UITextView) {
        if (textView.text.lengthOfBytes(using: String.Encoding.utf8) == 0) {
            placeholderLabel.text = "请输入您的反馈意见(150字以内)";
        }else{
            placeholderLabel.text = "";
        }
    }
    
    func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
        if (textView.text.lengthOfBytes(using: String.Encoding.utf8) > 151)
        {
            return false;
        }
        return true;
    }
    
    //MARK UIScrollView Delegate
    func scrollViewWillBeginDecelerating(_ scrollView: UIScrollView) {
        textView.resignFirstResponder()
    }
}
